import { NextRequest } from 'next/server';
import { withMiddleware } from '@/lib/middleware/entry';
import { prisma } from '@/lib/prisma';
import { authenticateRequest } from '@/utils/auth';

async function handler(req: NextRequest) {
    const { id } = await req.json();

    if (!id) {
        throw new Error('Missing content ID');
    }

    const content = await prisma.content.findUnique({
        where: { id },
        include: {
            cover: true,
                                agent: true,

            categories: {
                include: {
                    category: true,
                },
            },
        },
    });

    if (!content) {
        throw new Error('Content not found');
    }

    const simplifiedCategories = content.categories.map(cc => cc.category);

    return {
        ...content,
        categories: simplifiedCategories,
    };
}

export const POST = withMiddleware(handler);